package com.aji.winecommon.utills;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;

public class FileUtills {
    public static ResponseEntity<byte[]> transformFile(String filePath,String returnName) throws IOException {
        File file = new File(filePath);
        InputStream inputStream = new FileInputStream(file);

        // 设置响应头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        headers.setContentDispositionFormData("attachment", returnName);
        headers.setAccessControlExposeHeaders(Collections.singletonList("Content-Disposition"));

        // 读取文件内容到字节数组
        byte[] bytes = new byte[(int) file.length()];
        inputStream.read(bytes);

        // 返回字节数组作为响应
        return new ResponseEntity<>(bytes, headers, HttpStatus.OK);
    }
}
